<?php

namespace core\notice;


class ExceptionHandle
{

    private static $reciver = [];

    private static $defaultQueueList = [];

    private static function getQueueList()
    {
        return self::$defaultQueueList;
    }

    public static function enqueue($msg, $queueName, $reciver = null)
    {
        $reciver = empty($reciver) ? self::$reciver : $reciver;
        if (empty($reciver)) {
            return;
        }
        // 收集默认参数
        $args = [
            'GET:'. @json_encode($_GET),
            'POST:'. @json_encode($_POST)
        ];
        $msg = implode(PHP_EOL, $args). PHP_EOL. $msg;
        $data = ['msg' => $msg, 'reciver' => $reciver];
        Queue::enqueue($data, $queueName);
    }

    public static function noticeAll()
    {
        $queueList = self::getQueueList();
        if (empty($queueList)) {
            return;
        }
        foreach ($queueList as $queue) {
            $queueData = Queue::dequeueAll($queue);
            if (empty($queueData)) {
                continue;
            }
            $results = [];
            foreach ($queueData as $item) {
                $recivers = $item['reciver'];
                if (!empty($recivers)) {
                    foreach ($recivers as $reciver) {
                        if (key_exists($reciver, $results)) {
                            $results[$reciver][] = $item['msg'];
                        } else {
                            $results[$reciver] = [$item['msg']];
                        }
                    }
                }
            }
            // 每一条错误消息队列各自发送消息

        }
    }

    public static function sendMsg($msg, $reciver)
    {
        // 各自实现
    }

    public static function handle($e, $catogery, $defaultMsg)
    {

    }
}